package com.atwisdom.star.core.dsl.util;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.UuidTool;
import com.atwisdom.star.core.dsl.functionClass.BatchNumberDslService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 
 * @Date: 2023-09-04 14:29
 * 处理自动生成数据
 */
@Component
@Slf4j
@AllArgsConstructor
public class AutoGenUtil {

    private BatchNumberDslService batchNumberDslService;


    public JSONObject handleAutoGenValue(JSONObject targetValueJson) {
        targetValueJson.forEach((k, v)->{
            if (v instanceof String) {
                String valueStr = String.valueOf(v);
                String newValue = valueStr;
                int index = valueStr.indexOf("autogen.");
                if (valueStr.contains("autogen.") && index < 4) {
                    newValue = batchNumberDslService.fetchAutoGen(valueStr);
                } else if (valueStr.contains("autodate.")) {
                    String formatStr = valueStr.substring(valueStr.indexOf("autodate.") + "autodate.".length());
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatStr);
                    newValue = LocalDateTime.now().format(dtf);
                }else if(valueStr.contains("autouuid.")){
                    String  prefix= valueStr.substring(valueStr.indexOf("autoUuid.") + "autouuid.".length());
                    newValue = prefix+UuidTool.getUUID32();
                }
                targetValueJson.put(k, newValue);
            }
        });
        return targetValueJson;
    }

}